package com.chat.websocket.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chat.websocket.entity.dto.AddToFriendDTO;
import com.chat.websocket.entity.dto.CheckFriendDTO;
import com.chat.websocket.entity.dto.SearchUserDTO;
import com.chat.websocket.entity.enums.DeleteStatus;
import com.chat.websocket.entity.enums.TopStatus;
import com.chat.websocket.entity.enums.VerifyStatus;
import com.chat.websocket.entity.module.Friend;
import com.chat.websocket.entity.module.FriendVerify;
import com.chat.websocket.entity.vo.ChatListVO;
import com.chat.websocket.entity.vo.FriendListVO;
import com.chat.websocket.entity.vo.NewFriendVO;
import com.chat.websocket.mapper.FriendMapper;
import com.chat.websocket.mapper.FriendVerifyMapper;
import com.chat.websocket.service.FriendService;
import com.chat.websocket.util.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SunJingxuan
 * 2022 \ 08 \ 09
 */
@Slf4j
@Service
public class FriendServiceImpl implements FriendService {

	@Resource
	private FriendMapper friendMapper;

	@Resource
	private FriendVerifyMapper friendVerifyMapper;

	@Override
	public void addToFriend(AddToFriendDTO addToFriendDTO) {
		LambdaUpdateWrapper<FriendVerify> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.set(FriendVerify::getDeleteStatus, DeleteStatus.DELETED.getStatus()).eq(FriendVerify::getUserId, addToFriendDTO.getUserId()).eq(FriendVerify::getFriendUserId, addToFriendDTO.getFriendUserId());
		friendVerifyMapper.update(null, updateWrapper);
		friendVerifyMapper.insert(new FriendVerify(addToFriendDTO.getUserId(), addToFriendDTO.getFriendUserId(), addToFriendDTO.getVerifyMessage(), addToFriendDTO.getTopStatus() ? TopStatus.TOP.getStatus() : TopStatus.NOT_TOP.getStatus(), new Date(), new Date()));
	}

	@Override
	public List<FriendListVO> searchUserFriend(Integer userId) {
		return friendMapper.searchUserFriend(userId);
	}

	@Override
	public List<NewFriendVO> searchNewFriend(Integer userId) {
		return friendVerifyMapper.searchNewFriend(userId);
	}

	@Override
	public Boolean whetherHaveNewFriend(Integer userId) {
		return friendVerifyMapper.whetherHaveNewFriend(userId);
	}

	@Override
	public void checkFriend(CheckFriendDTO checkFriendDTO) {
		LambdaUpdateWrapper<FriendVerify> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
		lambdaUpdateWrapper.set(FriendVerify::getVerifyStatus, checkFriendDTO.getVerifyStatus()).eq(FriendVerify::getId, checkFriendDTO.getVerifyId());
		friendVerifyMapper.update(null, lambdaUpdateWrapper);
		if (checkFriendDTO.getVerifyStatus().equals(VerifyStatus.AGREE.getStatus())) {
			FriendVerify friendVerify = friendVerifyMapper.selectById(checkFriendDTO.getVerifyId());
			Date now = new Date();
			friendMapper.insert(new Friend(friendVerify.getUserId(), friendVerify.getFriendUserId(), now, friendVerify.getTopStatus(), now, now));
			friendMapper.insert(new Friend(friendVerify.getFriendUserId(), friendVerify.getUserId(), now, friendVerify.getTopStatus(), now, now));
		}
	}

	@Override
	public List<ChatListVO> searchUserChatList(Integer userId) {
		List<ChatListVO> chatListVOS = friendMapper.searchUserChatList(userId, null);
		chatListVOS.forEach(item -> {
			String[] time = item.getLastMessageTime().split(" ");
			// 最新一条消息是今天
			if (DateUtil.isToday(time[0])) {
				item.setLastMessageTime(time[1]);
			} else if (DateUtil.isYesterday(time[0])) {
				// 最新一条消息是昨天
				item.setLastMessageTime("昨天");
			} else if (DateUtil.isThisYear(time[0])) {
				try {
					// 今年的消息
					item.setLastMessageTime(DateUtil.yearMonthDayToMonthDay(time[0]));
				} catch (ParseException e) {
					log.error("查询聊天列表时间解析出错", e);
				}
			} else {
				item.setLastMessageTime(time[0]);
			}
		});
		if (!CollectionUtils.isEmpty(chatListVOS)) {
			friendMapper.updateChatListDeleteStatus(userId, chatListVOS.stream().map(ChatListVO::getUserId).collect(Collectors.toList()));
		}
		return chatListVOS;
	}

	@Override
	public void changeToTopStatus(Integer userId, Integer friendUserId) {
		friendMapper.changeToTopStatus(userId, friendUserId);
	}

	@Override
	public void changeChatListDeleteStatus(Integer userId, Integer friendUserId) {
		friendMapper.changeChatListDeleteStatus(userId, friendUserId);
	}

	@Override
	public List<ChatListVO> searchFriend(SearchUserDTO searchUserDTO) {
		return friendMapper.searchUserChatList(searchUserDTO.getUserId(), searchUserDTO.getUsername());
	}
}
